package cn.edu.stu.syllabus.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;

import java.util.List;

import butterknife.BindView;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.adapters.BannerImageHolderView;
import cn.edu.stu.syllabus.adapters.FunctionAdapter;
import cn.edu.stu.syllabus.base.BaseFragment;
import cn.edu.stu.syllabus.base.BaseQuickClickAdapter;
import cn.edu.stu.syllabus.repository.banner.local.BannerEntry;
import cn.edu.stu.syllabus.dialog.ThemeDialog;
import cn.edu.stu.syllabus.datas.FunctionItem;
import cn.edu.stu.syllabus.utils.DensityUtil;
import cn.edu.stu.syllabus.utils.Direction;
import cn.edu.stu.syllabus.utils.ListUtils;
import cn.edu.stu.syllabus.utils.ScreenOriUtil;

/**
 * Created by daidaijie on 2017/3/5.
 */

public class MainFragment extends BaseFragment implements MainContract.View {

    private static final String TAG = "MainFragment";

    private MainContract.Presenter mPresenter;

    FunctionAdapter mFunctionAdapter;
    BannerImageHolderView mBannerImageHolderView;
    BannerImageHolderView.BannerViewHolderCreator mBannerViewHolderCreator;

    @BindView(R.id.FunctionRecyclerView)
    RecyclerView mFunctionRecyclerView;
    @BindView(R.id.convenientBanner)
    ConvenientBanner<BannerEntry> mConvenientBanner;
    @BindView(R.id.functionTextView)
    TextView mFunctionTextView;
    @BindView(R.id.functionCardView)
    CardView mFunctionCardView;

    public static MainFragment newInstance() {
        MainFragment fragment = new MainFragment();
        return fragment;
    }


    @Override
    protected int getContentViewId() {
        return R.layout.fragment_main;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        initFuncCard();
        initBanner();
        mPresenter.subscribe();
    }

    @Override
    public void onDestroyView() {
        mPresenter.unsubscribe();
        super.onDestroyView();
    }

    private void initFuncCard() {
        FunctionItem syllabusFunctionItem = FunctionItem.FunctionItemsHolder.mSyllabusFunctionItems;
        FunctionAdapter.setFunctionToView(syllabusFunctionItem, mFunctionCardView, mFunctionTextView, Direction.LEFT);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(mActivity, 3);
        mFunctionAdapter = new FunctionAdapter(FunctionItem.FunctionItemsHolder.mFunctionItems);
        mFunctionRecyclerView.setLayoutManager(gridLayoutManager);
        mFunctionRecyclerView.setAdapter(mFunctionAdapter);
        mFunctionRecyclerView.setNestedScrollingEnabled(false);
        mFunctionAdapter.setOnItemClickListener(new BaseQuickClickAdapter.OnItemClickListener<FunctionItem>() {
            @Override
            public void onItemClick(FunctionItem item, int position) {
                if (item.getActivityClazz() != null) {
                    Intent intent = new Intent(mActivity, item.getActivityClazz());
                    MainFragment.this.startActivity(intent);
                    return;
                }
                final ThemeDialog themeDialog = new ThemeDialog();
                themeDialog.show(getChildFragmentManager(),
                        ThemeDialog.DIALOG_THEME_PICKER);
            }
        });

    }

    private void initBanner() {
        ViewGroup.LayoutParams layoutParams = mConvenientBanner.getLayoutParams();
        if (ScreenOriUtil.isPortrait()) {
            layoutParams.height = (deviceWidth - DensityUtil.dip2px(8.0f)) * 9 / 16;
        } else {
            layoutParams.height = deviceWidth / 2 * 9 / 16;
        }

        mBannerImageHolderView = new BannerImageHolderView(this);
        mBannerViewHolderCreator = new BannerImageHolderView.BannerViewHolderCreator(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mConvenientBanner.startTurning(3000);
    }

    @Override
    public void setBannerPage(List<BannerEntry> bannerEntries) {
        if (ListUtils.getListSizeSafety(bannerEntries) == 0) {
            return;
        }
        mConvenientBanner.setPages(mBannerViewHolderCreator, bannerEntries)
                .setPageIndicator(BannerImageHolderView.sIndicator)
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
        mConvenientBanner.setcurrentitem((int) (Math.random() * bannerEntries.size()));
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    public void setPresenter(@NonNull MainContract.Presenter presenter) {
        mPresenter = presenter;
    }
}
